package com.kgis.fm.common.component{
	import flash.events.Event;
	import flash.events.MouseEvent;

	import mx.controls.Button;
	import mx.controls.TextInput;

	[Event(name="buttonClick",type="flash.events.Event")]

	public class SelectableTextInput extends TextInput {
		private var _useSelect:Boolean = true;
		private var selectButton:Button;
		private var _winClass:Class;

		public function SelectableTextInput() {
			super();

			setStyle("paddingLeft",0);
			setStyle("paddingRight",0);
			setStyle("paddingTop",0);
			setStyle("paddingBottom",0);
			setStyle("horizontalGap",0);
			setStyle("verticalGap",0);
			setStyle("verticalAlign","bottom");
		}

		override protected function createChildren():void {
	    	super.createChildren();
			if(!selectButton) {
				selectButton=new Button();
				selectButton.label = "...";
				selectButton.setStyle("paddingLeft",0);
				selectButton.setStyle("paddingRight",0);
				selectButton.setStyle("paddingTop",0);
				selectButton.setStyle("paddingBottom",0);
				selectButton.setStyle("fontWeight","bold");
				selectButton.addEventListener(MouseEvent.CLICK,clickHandle);
			}

			if(useSelect){
				addChild(selectButton);
			}
	    }

	    override protected function updateDisplayList(unscaledWidth:Number,
													  unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);

	        selectButton.setActualSize(20,20);
	        selectButton.x = x+unscaledWidth-22;
	    }

		private function clickHandle(event:MouseEvent):void {
			if(!useSelect) return;

			dispatchEvent(new Event("buttonClick"));
		}

		public function get winClass():Class{
			return _winClass;
		}

		public function set winClass(value:Class):void {
			_winClass = value;
		}

		public function get useSelect():Boolean {
			return _useSelect;
		}

		public function set useSelect(value:Boolean):void {
			_useSelect = value;

			if(selectButton==null) return;
			if(value){
				if(!owns(selectButton))
					addChild(selectButton);
			}else{
				if(owns(selectButton))
					removeChild(selectButton);
			}
		}

	}
}